package short_semester.questionnaire_planet.service.editService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.MultiChoiceOptionDao;
import short_semester.questionnaire_planet.dao.MultiChoiceQuestionDao;
import short_semester.questionnaire_planet.dao.MultipleChoiceQuestionRecordDao;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.editRequest.EditQuestionBody;
import short_semester.questionnaire_planet.request.editRequest.OptionBody;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.response.editResponse.RelationResponse;

import java.util.*;

@Service
public class MultiChoiceQuestionService {
    @Autowired
    MultiChoiceQuestionDao questionDao;
    
    @Autowired
    MultiChoiceOptionDao optionDao;
    
    @Autowired
    QuestionSequenceDao sequenceDao;

    @Autowired
    MultipleChoiceQuestionRecordDao recordDao;
    
    @Autowired
    RelationService relationService;
    
    public List<MultiChoiceQuestion> getQuestions(Questionnaire questionnaire){
        List<MultiChoiceQuestion> questions = questionDao.findByQuestionnaire(questionnaire);
        for(MultiChoiceQuestion question:questions){
            question.setOptions(optionDao.findByMultiChoiceQuestionOrderBySequenceAsc(question));
        }
        
        return questions;
    }
    
    public MultiChoiceQuestion getQuestion(Integer questionId){
        MultiChoiceQuestion question = questionDao.getById(questionId);
        question.setOptions(optionDao.findByMultiChoiceQuestionOrderBySequenceAsc(question));
        return question;
    }

    public MultiChoiceQuestion getQuestionRandom(Integer questionId){
        MultiChoiceQuestion question = questionDao.getById(questionId);
        question.setOptions(optionDao.findByMultiChoiceQuestion(question));
        if(question.getIsRandom()==null){
            
        }else if(question.getIsRandom()){
            Collections.shuffle(question.getOptions());
        }
        return question;
    }

    public MultiChoiceQuestion getQuestion(MultiChoiceQuestion question){
        question.setOptions(optionDao.findByMultiChoiceQuestionOrderBySequenceAsc(question));
        return question;
    }

    public List<MultiChoiceQuestion> getQuestionInfo(Questionnaire questionnaire){
        return questionDao.findByQuestionnaire(questionnaire);
    }
    
    public CommonResponse getQuestionResponse(Integer id){
        CommonResponse response = new CommonResponse();
        try {
            MultiChoiceQuestion question = questionDao.getById(id);
            question.setOptions(optionDao.findByMultiChoiceQuestionOrderBySequenceAsc(question));
            response.setData(question);
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }

    public int addQuestion(Questionnaire questionnaire,Integer sequence,String qtype){
        MultiChoiceQuestion question = new MultiChoiceQuestion(qtype);
        question.setQuestionnaire(questionnaire);
        question.setSequence(sequence);
        question = questionDao.save(question);
        

        QuestionSequence questionSequence = new QuestionSequence();
        questionSequence.setQuestionnaire(questionnaire);
        questionSequence.setSequence(sequence);
        questionSequence.setType(question.getType());
        questionSequence.setQuestionId(question.getId());
        sequenceDao.save(questionSequence);
        return question.getId();
    }

    public int getQuestionNum(Questionnaire questionnaire){
        return questionDao.countByQuestionnaire(questionnaire);
    }
    
    public void deleteQuestion(Integer questionId){
        MultiChoiceQuestion question = questionDao.findById(questionId).get();
//        for (MultiChoiceOption option: optionDao.findByMultiChoiceQuestion(question)){
//            optionDao.delete(option);
//        }
        optionDao.deleteByMultiChoiceQuestion(question);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), question.getId());
        sequenceDao.delete(questionSequence);

        relationService.relationDao.deleteByQuestionIdAndType(questionId,question.getType());
        relationService.relationDao.deleteByRelationIdAndRelationType(questionId,question.getType());
        
        questionDao.delete(question);
    }
    
    public MultiChoiceQuestion getQuestionById(Integer questionId){
        return questionDao.getById(questionId);
    }
    
    public void setSequenceById(Integer questionId,Integer sequence){
        MultiChoiceQuestion question = questionDao.getById(questionId);
        question.setSequence(sequence);
        questionDao.save(question);

        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), questionId);
        questionSequence.setSequence(sequence);
        sequenceDao.save(questionSequence);
    }
    
    public CommonResponse editQuestion(EditQuestionBody body){
        CommonResponse response = new CommonResponse();
        
        try {

            MultiChoiceQuestion question = questionDao.getById(body.getId());
            question.setValueFrom(body);
            questionDao.save(question);
            
            List<MultiChoiceOption> options = optionDao.findByMultiChoiceQuestion(question);
            Map<Integer, MultiChoiceOption> optionMap = new HashMap<>();
            for (MultiChoiceOption option :options){
                optionMap.put(option.getId(),option);
            }
            
            int sequence = 0;
            for (OptionBody optionBody: body.getOptions()){
                MultiChoiceOption option = new MultiChoiceOption(optionBody);
                option.setMultiChoiceQuestion(question);
                option.setSequence(sequence);
                ++sequence;

                optionMap.remove(option.getId());
                
                optionDao.save(option);
            }
            
            for (MultiChoiceOption option: optionMap.values()){
                relationService.relationDao.deleteByOptionIdAndRelationType(option.getId(),question.getType());
                optionDao.delete(option);
            }

        }catch (Exception e){
            response.error(e.getMessage());
            e.printStackTrace();
        }
        
        return  response;
    }
    
    public void copyQuestion(Questionnaire questionnaire,Questionnaire oldQuestionnaire){
        for (MultiChoiceQuestion question : questionDao.findByQuestionnaire(oldQuestionnaire)) {
            MultiChoiceQuestion multiChoiceQuestion = new MultiChoiceQuestion(question);
            multiChoiceQuestion.setQuestionnaire(questionnaire);
            multiChoiceQuestion = questionDao.save(multiChoiceQuestion);

            
            QuestionSequence questionSequence = new QuestionSequence(multiChoiceQuestion);
            sequenceDao.save(questionSequence);

            for (MultiChoiceOption option : optionDao.findByMultiChoiceQuestion(question)) {
                MultiChoiceOption multiChoiceOption = new MultiChoiceOption(option);
                multiChoiceOption.setMultiChoiceQuestion(multiChoiceQuestion);
                multiChoiceOption = optionDao.save(multiChoiceOption);

            }
        }
    }

    //彻底删除题目，包括选项与答题
    public void completeDelete(MultiChoiceQuestion multiChoiceQuestion){
        recordDao.deleteByMultiChoiceQuestion(multiChoiceQuestion);
        optionDao.deleteByMultiChoiceQuestion(multiChoiceQuestion);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(multiChoiceQuestion.getType(), multiChoiceQuestion.getId());
        sequenceDao.delete(questionSequence);


        questionDao.delete(multiChoiceQuestion);
    }

    //删除题目对应的回答
    public void deleteAnswer(MultiChoiceQuestion multiChoiceQuestion){
        recordDao.deleteByMultiChoiceQuestion(multiChoiceQuestion);
    }

    //获取关联逻辑
    public RelationResponse getRelation(Integer questionId){
        MultiChoiceQuestion multiChoiceQuestion = questionDao.getById(questionId);
        return relationService.getRelation(multiChoiceQuestion);
    }
}
